package com.example.configuration;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.event.SimpleApplicationEventMulticaster;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

/**
 *
 * @author Administrator
 * @date 2019-12-30 0030
 */
@Component
public class ThreadPollConfiguration {

    @Bean("taskExecutor")
    public ExecutorService initTaskExecutor(){
        ThreadPoolExecutor executor= new ThreadPoolExecutor(5, 50,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(200), r -> {
                    return new Thread(r,"t_cus_"+r.hashCode());
                });
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy ());
        return executor;
    }

    @Bean("applicationEventMulticaster")
    public SimpleApplicationEventMulticaster init(@Qualifier("taskExecutor")Executor executor){
        SimpleApplicationEventMulticaster simpleApplicationEventMulticaster= new SimpleApplicationEventMulticaster();
        simpleApplicationEventMulticaster.setTaskExecutor(executor);
        return simpleApplicationEventMulticaster;
    }
}
